package top.kuxiao.crmserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.kuxiao.crmserver.pojo.*;
import top.kuxiao.crmserver.service.IAccountManagerService;
import top.kuxiao.crmserver.service.ICustomerService;
import top.kuxiao.crmserver.service.ISaleChanceService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.CookieUtil;
import top.kuxiao.crmserver.utils.ReportResult;
import top.kuxiao.crmserver.utils.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户信息管理 前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-17
 */
@Api(tags = "客户信息管理")
@CrossOrigin
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    IAccountManagerService accountManagerService;

    @Autowired
    CookieUtil cookieUtil;

    @Autowired
    ICustomerService customerService;

    @Autowired
    ISaleChanceService saleChanceService;


    @ApiOperation("报表")
    @RequestMapping(value = "report", method = RequestMethod.POST)
    public ReportResult report() {
        List<Report> customerContribute = customerService.getCustomerContribute();

        ArrayList<String> strings = new ArrayList<>();
        ArrayList<Integer> integers = new ArrayList<>();
        for (Report report : customerContribute) {
            strings.add(report.getName());
            integers.add(report.getValue());
        }

        return ReportResult.ok(strings, integers);
    }

    @Auth("AM,SE")
    @ApiOperation(value = "查询属于该账号的客户")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ResultUtil list(HttpServletRequest request) {

        IUser object = cookieUtil.getObject(request);

        List<Customer> list = new ArrayList<>();
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();

        if (object instanceof AccountManager) {
            AccountManager accountManager = (AccountManager) object;
            customerQueryWrapper.eq("customer_cid", accountManager.getId());

            list = customerService.list(customerQueryWrapper);

            for (Customer customer : list) {
                if (customer.getCustomerCid().equals(accountManager.getId())) {
                    customer.setAccountManager(accountManager);
                }
            }
        } else {
            list = customerService.list(customerQueryWrapper);
        }


        return ResultUtil.resObj(list);
    }

    /**
     * 添加客户
     */
    @Auth("AM")
    @ApiImplicitParam(name = "id", value = "机会的id")
    @ApiOperation(value = "添加客户", notes = "只有开发成功后开发客户,不需要客户名字、客户经理、状态")
    @RequestMapping(value = "add/{id}", method = RequestMethod.POST)
    public ResultUtil add(@PathVariable("id") Integer id, Customer customer, HttpServletRequest request) {


        SaleChance byId = saleChanceService.getById(id);

        customer.setCustomerName(byId.getScCustomerName());

        IUser object = cookieUtil.getObject(request);
        if (object instanceof AccountManager) {
            customer.setCustomerCid(((AccountManager) object).getId());
        }
        customer.setCustomerState("正常");


        if (customerService.save(customer)) {
            return ResultUtil.resMsgOK("添加成功");
        }
        return ResultUtil.resMsgError("添加失败");
    }

    @ApiOperation(value = "客户回显")
    @ApiImplicitParam(name = "id", value = "客户id")
    @RequestMapping(value = "toUpdate/{id}", method = RequestMethod.POST)
    public ResultUtil toUpdate(@PathVariable("id") Integer id) {

        Customer byId = customerService.getById(id);
        return ResultUtil.resObj(byId);

    }


    @ApiOperation(value = "更新客户", notes = "不需要客户经理、状态")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResultUtil update(Customer customer) {
        if (customerService.updateById(customer)) {
            return ResultUtil.resMsgOK("更新成功");
        }

        return ResultUtil.resMsgError("更新失败");
    }


    @ApiOperation(value = "批量删除客户")
    @ApiImplicitParam(name = "id", value = "要删除的id")
    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    public ResultUtil del(@PathVariable("id") Integer id) {

        try {
            if (customerService.removeById(id)) {
                return ResultUtil.resMsgOK("成功");
            }
        } catch (Exception e) {
            return ResultUtil.resMsgError("失败");
        }


        return ResultUtil.resMsgError("失败");
    }

}
